part of "../framework.dart";

/// 容纳一个点状物体（比如CustomPaint），并赋予它体积
class VolumeBox extends StatelessWidget {
  final double width;
  final double height;
  final Color? backgroundColor;
  final Widget child;
  const VolumeBox({
    required this.width,
    required this.height,
    required this.child,
    this.backgroundColor,
    super.key,
  });

  const VolumeBox.square(
      {required double size,
      required this.child,
      this.backgroundColor,
      super.key})
      : width = size,
        height = size;

  @override
  Widget build(BuildContext context) {
    // 本质是将点状物体放到Container中，此时物体居于左上角
    // 再对物体施加一个位移，让它正好处于Container正中间
    // 这样看起来就像是赋予了点状物体体积一样
    return Container(
      width: width,
      height: height,
      color: backgroundColor,
      child: Transform.translate(
        offset: Offset(width / 2, height / 2),
        child: child,
      ),
    );
  }
}
